﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ClientAppraise.ViewModels.Validator
{
    public sealed class ValidatorCodeAttribute : ValidationAttribute
    {
        int length = 5;
        public ValidatorCodeAttribute(int length = 5)
        {
            this.length = length;
        }
        protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
        {
            if (value == default) return ValidationResult.Success;

            var strValue = value.ToString();

            if (string.IsNullOrEmpty(strValue)) return ValidationResult.Success;


            if (!Regex.IsMatch(strValue, @"^([A-Za-z0-9]{" + $"{length}}})$"))
            {
                return new($"只能是长度为{length}的数字与字母组合");
            }

            return ValidationResult.Success;
        }
    }

}
